import {
  Graph,
  Shape,
  Cell,
  Model
} from '@antv/x6';


export default {

  getCell(id, graph) {
    let cells = graph.toJSON().cells
    let cell = null
    for (let i = 0; i < cells.length; i++) {
      let item = cells[i]
      if (item.id == id) {
        cell = item
        break
      }
    }
    return cell
  },
  initMount({
    node,
    height,
    scroller,
    size,
    factor
  }) {
    let mountNode = document.getElementById(node)
    let width = mountNode.scrollWidth - 8
    const graph = new Graph({
      container: mountNode,
      width: width,
      height: height || 350,
      autoResize: true,
      scroller: scroller || false,
      // snapline: true,
      translating: {
        restrict(view) {
          let cell = view.cell
          if (cell.isNode()) {
            if (cell.id == "mount") {
              return {
                x: cell.attrs.info.initX,
                y: 0,
                width: 0,
                height: 40
              }
            }
            return {
              x: 0,
              y: cell.attrs.info.initY,
              width: width,
              height: 40
            }
          }
        }
      },
      resizing: {
        enabled: (view) => {
          if (view.id == "mount") {
            return false
          }
          return true
        },
        restricted: true,
        maxHeight: 40,
        minHeight: 40
      },
      background: {
        color: '#fffbe6', // 设置画布背景颜色
      },
      grid: {
        visible: true,
        size: size || 10,
        type: 'doubleMesh',
        args: [{
            color: '#eee', // 主网格线颜色
            thickness: 1, // 主网格线宽度
          },
          {
            color: '#ddd', // 次网格线颜色
            thickness: 1, // 次网格线宽度
            factor: factor || 5, // 主次网格线间隔
          },
        ],
      },
    });
    return graph
  }
}
